var d = null;
$(function () {
    d = top.dialog.get(window);
    var keyword = $('#keyword').val();
    var setting = {
        view :{
            fontCss:getFont,
            nameIsHTML: true
        },
        data : {
            simpleData : {
                //是否之用简单数据
                enable : true,
                //对应json数据中的ID
                idKey : 'id',
                //对应json数据中的父ID
                pIdKey : 'pId',
                rootPId: "0"
            }
        },
        async: {
            type: "post",
            enable: true,
            url: "/dept/list",
            otherParam:['name',keyword]
        },
        callback:{
            onDblClick: zTreeOnDblClick
        }
    };

    $.fn.zTree.init($("#treeDemo"), setting);


    //keyword失焦事件
    $('#keyword').blur(function () {
        var text = $('#keyword').val();
        $.ajax({
            type:'post',
            data:{'name':text},
            dataType:'json',
            url:'/dept/list',
            success:function (data) {
                $.fn.zTree.init($("#treeDemo"), setting ,data);
            }
        });
    });

});

/**
 * 节点双击选中
 */
function zTreeOnDblClick(event, treeId, treeNode) {
    if (treeNode.isParent) {
        showErrDialog("请选择叶子节点");
        return;
    }
    d.close(treeNode.name); // 关闭（隐藏）对话框
    d.remove(); // 主动销毁对话框
    return false;
}

function getFont(treeId, node) {
    var text = $('#keyword').val();
    if (text && node.name.indexOf(text) > -1) {
        return {color:"red"};
    }
    return  {};
}

function showErrDialog(message) {
    var d = dialog({
        id: 'align-test',
        title: '消息',
        content: message,
        okValue: '确 定',
        align: $(this).data('align'),
        ok: function() {}
    });
    d.show();
}


